#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <cstdio>

using namespace std;

const int N = 50010;
int q[N], d[N];
int find(int t)
{
    if (t != q[t])
    {
        int u = q[t];
        q[t] = find(q[t]);
        d[t] += d[u];
    }
    return q[t];
}

int main()
{
    int n, k;
    int num = 0;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++)
        scanf("%d", &q[i]);
    while (k--)
    {
        int t, x, y;
        scanf("%d%d%d", &t, &x, &y);
        int px = find(x), py = find(y);
        if (t == 1)
        {
            if (px != py)
            {
                q[px] = py;
                d[x] += d[py] - d[px];
            }
            else if (px == py && (d[y] - d[x]) % 3)
                num++;
        }
        else
        {
            if (px != py)
            {
                q[px] = py;
                d[x] += d[py] - d[px] + 1;
            }
            else if (px == py && (d[y] - d[x] - 1) % 3)
                num++;
        }
        printf("%d", num);
    }
    return 0;
}